//
//  ZCLoginCollectionCell.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/26.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCLoginCollectionCell.h"
#import "ZCLoginInfoModel.h"

@interface ZCLoginCollectionCell ()<UITextFieldDelegate>

@property (strong, nonatomic) UITextField *accountTF;
@property (strong, nonatomic) UITextField *passwordTF;
@property (strong, nonatomic) UIButton *loginButton;
@property (strong, nonatomic) UIButton *forgetButton;

@end

@implementation ZCLoginCollectionCell


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
    }
    return self;
}

-(void)setupUI{
    
    self.accountTF = [UITextField textfieldWithPlaceholder:@"" backgroundImage:@"login_rgister_textfield_bg" fontSize:15 textColor:[UIColor whiteColor] delegate:self];

    self.passwordTF = [UITextField textfieldWithPlaceholder:@"" backgroundImage:@"login_rgister_textfield_bg" fontSize:15 textColor:[UIColor whiteColor] delegate:self];

    self.loginButton = [UIButton buttonWithBackgroundImage:@"login_register_button" selectedImage:nil title:nil fontSize:17 tintColor:[UIColor whiteColor] action:@selector(loginButtonAction:) target:self cornerRadius:5 highlighted:YES];
    [_loginButton setBackgroundImage:[UIImage imageNamed:@"login_register_button_click"] forState:UIControlStateHighlighted];
    
    self.forgetButton = [UIButton buttonWithBackgroundImage:nil selectedImage:nil title:@"忘记密码" fontSize:13 tintColor:nil action:@selector(forgetButtonAction) target:self cornerRadius:0 highlighted:YES];
    _forgetButton.titleLabel.textAlignment = NSTextAlignmentRight;

    
    [self.contentView addSubview:_accountTF];
    [self.contentView addSubview:_passwordTF];
    [self.contentView addSubview:_loginButton];
    [self.contentView addSubview:_forgetButton];
    
    [_accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.centerX.equalTo(self);
        make.leading.equalTo(self).offset(40);
        make.trailing.equalTo(self).offset(-40);
        make.height.mas_equalTo(40);
        
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_accountTF.mas_bottom).offset(0.5);
        make.centerX.equalTo(_accountTF);
        make.width.equalTo(_accountTF);
        make.height.equalTo(_accountTF);
    }];
    
   [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(_passwordTF.mas_bottom).offset(30);
       make.centerX.equalTo(_passwordTF);
       make.width.equalTo(_passwordTF);
   }];
    
    [_forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_loginButton.mas_bottom).offset(20);
        make.trailing.equalTo(_loginButton);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(20);
    }];
    
}

-(void)setModel:(ZCLoginInfoModel *)model{
    _model = model;
    
    
    self.accountTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:model.UserAccount attributes:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
    self.accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.accountTF.keyboardType = UIKeyboardTypeNumberPad;

    self.passwordTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:model.UserPassword attributes:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [self.loginButton setTitle:model.ButtonTitle forState:UIControlStateNormal];
    self.forgetButton.hidden = [model.ButtonTitle hasPrefix:@"注"];

}

-(void)loginButtonAction:(UIButton*)sender{
    
    if ([sender.titleLabel.text isEqualToString:@"登录"]) {
        ZCLog(@"登录");
    }else{
        ZCLog(@"注册");
    }
}

-(void)forgetButtonAction{
    [self endEditing:YES];
    ZCLog(@"忘记密码");
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

//MARK: TextField代理方法
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (textField == self.accountTF) {
        self.accountTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.model.UserAccount attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    }else{
        
        self.passwordTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.model.UserPassword attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    }
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField == self.accountTF) {
        
        self.accountTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.model.UserAccount attributes:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
    }else{
        
        self.passwordTF.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.model.UserPassword attributes:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
    }
    
}

@end
